/*
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: SettingActivity.java
 *   @Date: 18-11-29 下午4:05
 *   @Author: ppzhao
 *   @Description:
 *   @Copyright（C）: 2018 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 */

package io.bhex.app.account.ui;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import io.bhex.app.BuildConfig;
import io.bhex.app.R;
import io.bhex.app.account.event.LocaleChangeEvent;
import io.bhex.app.account.presenter.SettingPresenter;
import io.bhex.app.base.BaseActivity;
import io.bhex.app.utils.DialogUtils;
import io.bhex.app.utils.VersionUpdateUtil;
import io.bhex.app.view.TopBar;
import io.bhex.baselib.utils.DevicesUtil;
import io.bhex.baselib.utils.MD5Utils;
import io.bhex.baselib.utils.ToastUtils;
import io.bhex.sdk.UrlsConfig;
import io.bhex.sdk.account.UserInfo;
import io.bhex.sdk.data_manager.RateAndLocalManager;

/**
 * ================================================
 * 描   述：设置
 * ================================================
 */

public class SettingActivity extends BaseActivity<SettingPresenter, SettingPresenter.SettingUI> implements SettingPresenter.SettingUI, View.OnClickListener {

    private int clickCount = 0;
    private TopBar topBar;

    @Override
    protected int getContentView() {
        return R.layout.activity_setting_layout;
    }

    @Override
    protected SettingPresenter createPresenter() {
        return new SettingPresenter();
    }

    @Override
    protected SettingPresenter.SettingUI getUI() {
        return this;
    }

    @Override
    protected void onResume() {
        super.onResume();
        initView();
        if (UserInfo.isLogin()) {
            viewFinder.find(R.id.btn_logout).setVisibility(View.VISIBLE);
        } else {
            viewFinder.find(R.id.btn_logout).setVisibility(View.GONE);
        }
    }

    @Override
    protected void initView() {
        super.initView();

        topBar = viewFinder.find(R.id.topBar);
        this.setTopbarNight(topBar);

        viewFinder.textView(R.id.setting_language_select_tx).setText(RateAndLocalManager.GetInstance(this).getCurLocalKind().name);
        viewFinder.textView(R.id.setting_exchange_rate_select_tx).setText(RateAndLocalManager.GetInstance(this).getCurRateKind().name);
        viewFinder.textView(R.id.setting_version_tx).setText(DevicesUtil.getAppVersionName(this));
        TextView gitVersion = viewFinder.textView(R.id.setting_git_version);
        String gitCommitId = BuildConfig.GIT_COMMIT_ID;
        if (!TextUtils.isEmpty(gitCommitId)) {
            if (gitCommitId.length() > 6) {
                gitCommitId = gitCommitId.substring(0, 6);
            }
            gitVersion.setText("(" + gitCommitId + ")");
        }

    }

    @Override
    protected void addEvent() {
        super.addEvent();
        viewFinder.find(R.id.setting_exchange_rate_rela).setOnClickListener(this);
        viewFinder.find(R.id.setting_language_rela).setOnClickListener(this);
        viewFinder.find(R.id.setting_cache_rela).setOnClickListener(this);
        viewFinder.find(R.id.setting_version_rela).setOnClickListener(this);
        viewFinder.find(R.id.btn_logout).setOnClickListener(this);

        EventBus.getDefault().register(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.setting_exchange_rate_rela:
                //汇率
                startActivity(new Intent().setClass(this, RateSelectActivity.class));
                break;
            case R.id.setting_language_rela:
                //语言
                startActivity(new Intent().setClass(this, LocalLangSelectActivity.class));

                break;
            case R.id.setting_version_rela:

                VersionUpdateUtil.checkVersionUpdate(this, false);

                clickCount++;
                if (clickCount >= 5) {
                    String tip = MD5Utils.encode(UrlsConfig.API_SERVER_URL + "bhex.com");
                    if (!TextUtils.isEmpty(tip) && tip.length() > 10)
                        ToastUtils.showLong(this, tip.substring(0, 10));
                    clickCount = 0;
                }
                break;
            case R.id.setting_cache_rela:
                //清楚缓存

                break;
            case R.id.btn_logout:
                //退出
                DialogUtils.showDialog(this, "", getString(R.string.string_are_you_sure_logout), getString(R.string.string_sure), getString(R.string.string_cancel), true, new DialogUtils.OnButtonEventListener() {
                    @Override
                    public void onConfirm() {
                        getPresenter().logout();
                    }

                    @Override
                    public void onCancel() {

                    }
                });

                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void changeLanguage(LocaleChangeEvent changeEvent){
        misNeedRomve = 0;
        //recreate();
        //restartApp();
    }
}
